/* readlines.c - read all lines and saves in a buffer */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct buffer {
  const char *line;
  struct buffer *next;
};

struct buffer *
new_line(const char *line)
{
  struct buffer *buf = malloc(sizeof(struct buffer));
  buf->line = line;
  buf->next = NULL;
  return buf;
}

struct buffer *
insert_line(struct buffer *buf, const char *line)
{
  if (buf == NULL)
    return new_line(line);
  struct buffer *new = new_line(line);
  new->next = buf;
  return new;
}

main()
{
  struct buffer *buf = NULL;
  struct buffer *b;

  char line[BUFSIZ];
  
  for (; fgets(line, BUFSIZ, stdin); line == NULL)
    buf = insert_line(buf, strdup(line));

#if 1
  for (b=buf; b; b = b->next)
    printf("%s", b->line);
#endif
}
